// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: hyuan

#include "coin2/exchange/phemex_futures/api_order/native_rest_async_asio_subscriber.h"

namespace coin2::exchange::phemex_futures::api_order {

void PhemexFuturesRestAsyncAsioSubscriber::QueryAccountInfo(const std::string& currency) {
  RestParams params;
  params.set_currency(currency);
  auto http_context = rest_client()->QueryAccountInfo(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&PhemexFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void PhemexFuturesRestAsyncAsioSubscriber::QueryOpenOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryOpenOrders(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&PhemexFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void PhemexFuturesRestAsyncAsioSubscriber::QueryHistoryOrders(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryHistoryOrders(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&PhemexFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void PhemexFuturesRestAsyncAsioSubscriber::QueryFills(const std::string& native_symbol) {
  RestParams params;
  params.set_symbol(native_symbol);
  auto http_context = rest_client()->QueryFills(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&PhemexFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void PhemexFuturesRestAsyncAsioSubscriber::QueryTransfer(const std::string& ccy) {
  RestParams params;
  params.set_currency(ccy);
  auto http_context = rest_client()->QueryTransfer(params);
  if (!http_context) return;
  http_context->handler =
      std::bind(&PhemexFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void PhemexFuturesRestAsyncAsioSubscriber::SubscribeAsync() {
  auto native_currencies = symcache_->GetCurrenciesNativeUpper();

  AddPeriodicTask(
      std::bind(
          &PhemexFuturesRestAsyncAsioSubscriber::QueryAccountInfo,
          this,
          std::placeholders::_1),
      config_.query_account_balance_period_sec(),
      "query_account_info",
      native_currencies);

  auto symbol_list = native_symbols();

  AddPeriodicTask(
      std::bind(
          &PhemexFuturesRestAsyncAsioSubscriber::QueryOpenOrders,
          this,
          std::placeholders::_1),
      config_.query_open_orders_period_sec(),
      "query_open_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(
          &PhemexFuturesRestAsyncAsioSubscriber::QueryHistoryOrders,
          this,
          std::placeholders::_1),
      std::bind(
          &PhemexFuturesRestAsyncAsioSubscriber::SkipQuery,
          this,
          "query_history_orders",
          std::placeholders::_1),
      config_.query_cancel_orders_period_sec(),
      "query_history_orders",
      symbol_list);

  AddPeriodicTask(
      std::bind(&PhemexFuturesRestAsyncAsioSubscriber::QueryFills, this, std::placeholders::_1),
      std::bind(
          &PhemexFuturesRestAsyncAsioSubscriber::SkipQuery,
          this,
          "query_fills",
          std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fills",
      symbol_list);
}

}  // namespace coin2::exchange::phemex_futures::api_order
